﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using System.Threading;

namespace ProjectPacker
{
    public static class PackerClass
    {
        public static String AppPath = Application.StartupPath;
        public static ProgressForm pf; public static List<String> items; public static UpdateInfo ui = new UpdateInfo();
        public static void Pack(String UpdateInfoSample, String ConfigFile, String Date, String PackFilePath, String InfoFileName)
        {
            String DirPath;
            DirPath = Path.GetDirectoryName(InfoFileName);
            if (!Directory.Exists(DirPath))
            {
                Directory.CreateDirectory(DirPath);
            }
            if (!Directory.Exists(PackFilePath))
            {
                Directory.CreateDirectory(PackFilePath);
            }
            UpdateInfo.Load(ref ui, UpdateInfoSample);
            ui.SetDate(Date);
            Directory.Delete(MyConfig.TempFolder, true);
            Directory.CreateDirectory(MyConfig.TempFolder);
            items = DataManager.StringListLoad(ConfigFile);
            MessageSender.Progress_Total = items.Count + 4;
            MessageSender.Progress_Done = 0;
            Thread formThread = new Thread(ShowForm);
            formThread.Start();
            Thread workerThread = new Thread(RealPackThread);
            workerThread.Start(PackFilePath + "|" + InfoFileName);
            workerThread.Join();
        }
        public static void Pack(String UpdateInfoSample, String ConfigFile, String PackFilePath, String InfoFileName)
        {
            String DirPath;
            DirPath = Path.GetDirectoryName(InfoFileName);
            if (!Directory.Exists(DirPath))
            {
                Directory.CreateDirectory(DirPath);
            }
            if (!Directory.Exists(PackFilePath))
            {
                Directory.CreateDirectory(PackFilePath);
            }
            UpdateInfo.Load(ref ui, UpdateInfoSample);
            ui.SetDefault();
            Directory.Delete(MyConfig.TempFolder, true);
            Directory.CreateDirectory(MyConfig.TempFolder);
            items = DataManager.StringListLoad(ConfigFile);
            MessageSender.Progress_Total = items.Count + 4;
            MessageSender.Progress_Done = 0;
            Thread formThread = new Thread(ShowForm);
            formThread.Start();
            Thread workerThread = new Thread(RealPackThread);
            workerThread.Start(PackFilePath + "|" + InfoFileName);
            workerThread.Join();
        }
        public static void Pack(String UpdateInfoSample, String ConfigFile, String Date)
        {
            UpdateInfo.Load(ref ui, UpdateInfoSample);
            ui.SetDate(Date);
            Directory.Delete(MyConfig.TempFolder, true);
            Directory.CreateDirectory(MyConfig.TempFolder);
            items = DataManager.StringListLoad(ConfigFile);
            MessageSender.Progress_Total = items.Count + 4;
            MessageSender.Progress_Done = 0;
            Thread formThread = new Thread(ShowForm);
            formThread.Start();
            Thread workerThread = new Thread(RealPackThread);
            workerThread.Start("");
            workerThread.Join();
        }
        public static void Pack(String UpdateInfoSample, String ConfigFile)
        {
            UpdateInfo.Load(ref ui, UpdateInfoSample);
            ui.SetDefault();
            Directory.Delete(MyConfig.TempFolder, true);
            Directory.CreateDirectory(MyConfig.TempFolder);
            items = DataManager.StringListLoad(ConfigFile);
            MessageSender.Progress_Total = items.Count + 4;
            MessageSender.Progress_Done = 0;
            Thread formThread = new Thread(ShowForm);
            formThread.Start();
            Thread workerThread = new Thread(RealPackThread);
            workerThread.Start("");
            workerThread.Join();
        }
        public static void RealPackThread(object sender)
        {
            String PackFilePath;
            String InfoFileName;
            if (((String)sender) != null)
            {
                String[] FilePath = ((String)sender).Split('|');
                PackFilePath = FilePath[0];
                InfoFileName = FilePath[1];
            }
            else
            {
                PackFilePath = "";
                InfoFileName = "";
            }
            PathWorker.PackPathCreate(items);
            Packer7z.Pack(ui.NewVersion + ".7z");
            MessageSender.Progress_Done++;
            if (File.Exists(PackFilePath + ui.FileName))
            {
                File.Delete(PackFilePath + ui.FileName);
            }
            MessageSender.Progress_Done++;
            File.Move(ui.NewVersion + ".7z", PackFilePath + ui.FileName);
            MessageSender.Progress_Done++;
            ui.FileLength = (new FileInfo(PackFilePath + ui.FileName)).Length;
            UpdateInfo.Save(ui, InfoFileName);
            MessageSender.Progress_Done++;
        }
        public static void ShowForm()
        {
            pf = new ProgressForm();
            pf.ShowDialog();
        }
    }
}
